package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.mapper.WmMaterialMapper;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.service.MaterialService;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements MaterialService {


    @Value("${fdfs.url}")
    private String baseUrl;
    @Autowired
    private FastDFSClient fastDFSClient;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        String s="";
        if (multipartFile==null) return paramError;
        try {
            s=fastDFSClient.uploadFile(multipartFile);


        } catch (IOException e) {
            e.printStackTrace();
        }
        Integer userid = WmThreadLocalUtils.getUserid();
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(s);
        wmMaterial.setUserId(WmThreadLocalUtils.getUserid());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        wmMaterial.setUrl(baseUrl+userid);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto==null) return paramError;
        Integer userid = WmThreadLocalUtils.getUserid();
        if (userid==null)return needLogin;
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,userid);
        if (dto.getIsCollection()!=null)wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
//      Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getUserId,userid).eq(WmMaterial::getIsCollection,dto.getIsCollection()==null?2:dto.getIsCollection());
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        ResponseResult pageResult = selectFindAll(dto, wrapper);
        List<WmMaterial> data = (List<WmMaterial>) pageResult.getData();
        List<WmMaterial> collect = data.stream().map(wmMaterial -> {
            wmMaterial.resetUrl(baseUrl + wmMaterial.getUrl());
            return wmMaterial;
        }).collect(Collectors.toList());
        List<String> collect1 = data.stream().map(e -> baseUrl + e.getUrl()).collect(Collectors.toList());
        pageResult.setData(collect);
        return pageResult;
    }
}
